package com.molyfun.parents.activity.report.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.activity.report.view.ReportDetailView;
import com.molyfun.parents.rest.bean.ReportDetailResponse;
import com.molyfun.parents.utils.TimeUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:9/19/16.
 * 课程
 */
public class ReportDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private static final int TYPE_HEADER = 1001;
    private static final int TYPE_CONTENT = 1002;
    private static final int TYPE_FOTTER = 1003;
    private Context mContext;
    private long timeBegin;
    private long timeEnd;
    private String remark;

    private String mTimeInterval;

    private List<ReportDetailResponse.ReportCourseBean> mDataList;

    private HashMap<Integer, Integer> mIsOpenMap = new HashMap<>();

    public ReportDetailAdapter(Context mContext) {
        this.mContext = mContext;
    }

    public void initData(ReportDetailResponse.ReportDetailBean reportDetailBean){
        this.mDataList = reportDetailBean.getReportcontentlist();
        timeBegin = reportDetailBean.getBegindatetime();
        timeEnd = reportDetailBean.getEnddatetime();
        remark = reportDetailBean.getCommentall();
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(viewType == TYPE_HEADER){
            return new ReportDetailHeaderViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_report_header,parent,false));
        }else if(viewType == TYPE_FOTTER){
            return new ReportDetailFotterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_report_fotter,parent,false));
        }else{
            return new ReportDetailCellViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_report_detail_cell,parent,false));
        }
    }



    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case TYPE_HEADER:
                ReportDetailHeaderViewHolder reportDetailHeaderViewHolder = (ReportDetailHeaderViewHolder) holder;
                mTimeInterval = TimeUtils.getDateByTimeMillis(timeBegin) + "~" + TimeUtils.getDateByTimeMillis(timeEnd);
                reportDetailHeaderViewHolder.mTimeInterval.setText(mTimeInterval);
                break;
            case TYPE_CONTENT:
                ReportDetailCellViewHolder reportDetailCellViewHolder = (ReportDetailCellViewHolder) holder;
                int positionInfoInt = 0;
                if(mIsOpenMap.containsKey(position)){
                    positionInfoInt = mIsOpenMap.get(position);
                }
                reportDetailCellViewHolder.mReportDetailView.initData(position, mDataList.get(position - 1), positionInfoInt, new ReportDetailView.OnReportDetailItemClick() {
                    @Override
                    public void onDetailItemClick(int position, int showInfoInt) {
                        mIsOpenMap.put(position, showInfoInt);
                        notifyItemChanged(position);
                    }
                });
                break;
            case TYPE_FOTTER:
                ReportDetailFotterViewHolder reportDetailFotterViewHolder = (ReportDetailFotterViewHolder) holder;
                if(TextUtils.isEmpty(remark)){
                    reportDetailFotterViewHolder.mReportRemark.setVisibility(View.GONE);
                }else{
                    reportDetailFotterViewHolder.mReportRemark.setVisibility(View.VISIBLE);
                    reportDetailFotterViewHolder.mReportRemark.setText(remark);
                }
                break;
        }

    }

    public String getTimeInterval(){
        return mTimeInterval;
    }


    @Override
    public int getItemCount() {
        if(mDataList == null){
            return 0;
        }
        if(TextUtils.isEmpty(remark)){
            return mDataList.size() + 1;
        }else{
            return mDataList.size() + 2;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if(position == 0){
            return TYPE_HEADER;
        }else if(position == mDataList.size() + 1){
            return TYPE_FOTTER;
        }else{
            return TYPE_CONTENT;
        }
    }

    class ReportDetailCellViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.report_detail_layout)
        ReportDetailView mReportDetailView;

        public ReportDetailCellViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    class ReportDetailHeaderViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.time_interval)
        TextView mTimeInterval;

        public ReportDetailHeaderViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

    class ReportDetailFotterViewHolder extends RecyclerView.ViewHolder {

        @InjectView(R.id.report_remark)
        TextView mReportRemark;

        public ReportDetailFotterViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }
    }

}
